using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace GameEditor.Common.Controls
{
    public class WhitenessPicker : Control
    {
        private const int VPadding = 10;
        public WhitenessPicker()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            GenerateWhitenessMap(Color.Black);
        }
        private Bitmap bitmap = null;


        private int whiteness;

        private Color baseColor;

        public Color BaseColor
        {
            get { return baseColor; }
            set { baseColor = value; }
        }

        public Color Color
        {
            get
            {
                float v = whiteness / 255.0f;


                return Color.FromArgb(
                    MathHelper.Clamp(0, 255, baseColor.R + whiteness),
                    MathHelper.Clamp(0, 255, baseColor.G + whiteness),
                    MathHelper.Clamp(0, 255, baseColor.B + whiteness));

            }
        }


        public event EventHandler ColorChanged;

        public int Whiteness
        {
            get { return whiteness; }
            set { whiteness = value; }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            Rectangle imageRect = new Rectangle(0, VPadding, bitmap.Width, Height - 2 * VPadding);
            g.DrawImage(bitmap, imageRect);

            //draw arrow
            int arrowTop = MathHelper.IntervalMap(
                whiteness,
                new GameEditor.Common.Range<int>(0, 255),
                new GameEditor.Common.Range<int>(VPadding, Height - VPadding)
                );

            Point pt1 = new Point(bitmap.Width, arrowTop);
            Point pt2 = new Point(pt1.X + 10, pt1.Y - 5);
            Point pt3 = new Point(pt1.X + 10, pt1.Y + 5);

            using (GraphicsPath path = new GraphicsPath())
            {
                path.StartFigure();
                path.AddLine(pt1, pt2);
                path.AddLine(pt2, pt3);
                path.AddLine(pt3, pt1);
                path.CloseFigure();
                g.FillPath(Brushes.White, path);
            }
            g.DrawLine(Pens.Black, pt1, pt2);
            g.DrawLine(Pens.Black, pt1, pt3);
            g.DrawLine(Pens.Black, pt2, pt3);

        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.Button == MouseButtons.Left)
            {
                UpdateParameter(e.Location);
            }
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                UpdateParameter(e.Location);
            }
        }
        private void UpdateParameter(Point location)
        {
            whiteness = MathHelper.IntervalMap
                (location.Y,
                new GameEditor.Common.Range<int>(VPadding, Height - VPadding),
                new GameEditor.Common.Range<int>(0, 255));
            if (whiteness < 0)
                whiteness = 0;
            if (whiteness > 255)
                whiteness = 255;
            if (ColorChanged != null)
                ColorChanged(this, EventArgs.Empty);
            Invalidate();
        }


        public void GenerateWhitenessMap(Color color)
        {
            if (bitmap != null)
                bitmap.Dispose();
            baseColor = color;
            bitmap = new Bitmap(20, 152, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (LinearGradientBrush brush = new LinearGradientBrush(
                    new Point(0, 0), new Point(0, 152), color, Color.White))
                {
                    g.FillRectangle(brush, new Rectangle(Point.Empty, bitmap.Size));
                }
            }
            if (ColorChanged != null)
                ColorChanged(this, EventArgs.Empty);
        }
    }
}
